package com.mall.service.authority.controller;

import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsAdmin;
import com.mall.model.domain.UmsAdmin;
import com.mall.model.domain.UmsRole;
import com.mall.model.dto.InfoDto;
import com.mall.model.dto.MyPage;
import com.mall.service.authority.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminController
 * @Description 用户登录接口
 * @Author dcq
 * @Date 2023/3/11 16:19
 * @Version 1.0
 */

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //登录
    @PostMapping("/login")
    public CommonResult login(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
        Map token = adminService.loginSei(username, password);
        return CommonResult.success(token);
    }

    //获取当前用户信息
    @GetMapping("/info")
    public CommonResult<InfoDto> info(String name) {
        InfoDto dao = adminService.infoSei(name);
        return CommonResult.success(dao);
    }

    //调试删除指定用户信息
    @PostMapping("/delete/{id}")
    public CommonResult delect(@PathVariable Integer id) {
        adminService.deleteSei(id);
        return CommonResult.success(null, "删除成功");
    }

    //根据用户名或姓名分页获取用户列表
    @GetMapping("/list")
    public CommonResult<CommonPage> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         String keyword) {
        CommonPage myPage = adminService.listSei(pageNum, pageSize, keyword);
        return CommonResult.success(myPage);
    }

    @GetMapping("/{id}")
    public UmsAdmin getId(@PathVariable String id) {
        UmsAdmin admin = adminService.getId(id);
        return admin;
    }

    /**
     * 员工退出
     */
    @PostMapping("/logout")
    public CommonResult<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前用户的id
        request.getSession().removeAttribute("umsAdmin");
        return CommonResult.success("登陆成功");
    }

    @GetMapping("/refreshToken")
    public CommonResult<String> lo(HttpServletRequest request) {

        return CommonResult.success("退出成功");
    }

    @PostMapping("/register")
    public CommonResult register(@RequestBody UmsAdmin umsAdmin) {
        String register = adminService.register(umsAdmin);
        if (null!=register){
            return CommonResult.error(register);
        }
        return CommonResult.success(1,"新增成功");
    }

    @PostMapping("/role/update")
    public CommonResult RoleUpdate(@RequestParam("adminId") Long adminId, @RequestParam("roleIds") List<Long> roleIds) {
         adminService.updateRoleById(adminId, roleIds);
         return CommonResult.success(1,"修改成功");
    }

//    @GetMapping("role/{id}")
//    public CommonResult<String> RoleAdminId(Integer adminId) {
//
//        return CommonResult.success("退出成功");
//    }

    //修改指定用户信息
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody UmsAdmin umsAdmin) {
        String update = adminService.update(id, umsAdmin);
        if (update!=null){
            return CommonResult.error(update);
        }
        return CommonResult.success(1,"修改成功");
    }

    //修改用户启用,禁用状态
    @PostMapping("/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Long id, Integer status) {

         adminService.updateStatus(id, status);
        return CommonResult.success(1,"修改成功");
    }

    //根据id查询用户分配角色
    @GetMapping("/role/{adminId}")
    public CommonResult<List<UmsRole>> getRolesInfo(@PathVariable Long adminId) {
        List<UmsRole> umsRoles = adminService.getRolesInfo(adminId);
        return CommonResult.success(umsRoles);
    }
}